package project_phase1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Contact {
	public static final int FIELD_FIRSTNAME = 1;
	public static final int FIELD_MIDDLENAME = 2;
	public static final int FIELD_LASTNAME = 3;
	public static final int FIELD_FULLNAME = 4;
	public static final int FIELD_DOB = 5;

	private String firstName;
	private String lastName;
	private String middleName = null;
	//DOB is now required
	private Date dob = null;
	
	private final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
	
	public Contact(String firstName, String lastName, String dob) {
		if(firstName == null || firstName.trim().length() < 1)
			throw new IllegalArgumentException("First name is required!");
		if(lastName == null || lastName.trim().length() < 1)
			throw new IllegalArgumentException("Last name is required!");
		if(dob != null) {
			try {
				this.dob = dateFormat.parse(dob);
			} catch (ParseException e) {
				throw new IllegalArgumentException("DOB should be string in format of mm/dd/yyyy !");
			}
		} else
			throw new IllegalArgumentException("DOB should be string in format of mm/dd/yyyy !");
		this.firstName = firstName;
		this.lastName = lastName;
	}
	
	public Contact(String firstName, String middleName, String lastName, String dob) {
		this(firstName, lastName, dob);
		this.middleName = middleName;
	}

	public String getContactName() {
		String ret = firstName;
		if(middleName != null && middleName.trim().length() > 0) 
			ret += " " + middleName;
		ret += " " + lastName;
		return ret;
	}
	
	public String getFormattedDOB() {
		if(dob != null) {
			return dateFormat.format(dob);
		} else
			return "";
	}
	
	public String toString() {
		String ret = this.getClass().getSimpleName() + " : " + getContactName();
		ret += "\n DOB: " + getFormattedDOB();
		return ret;
	}

	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}

	public String getMiddleName() {
		return middleName;
	}
	
	public Date getDOB() { 
		return dob;
	}
}